from django.http import JsonResponse

from main.models import Group, Book


def get_information_about_groups(request):
    """ Получаем информацию о группах """

    def get_list_of_groups(request):
        list_of_groupsORM = Group.objects.values_list('title', 'slug', 'description', 'image')
        return JsonResponse({number: group for number, group in enumerate(list_of_groupsORM, 0)})


    def get_group(request):
        group_slug = request.GET.get('group')
        books = Book.objects.filter(group__slug=group_slug).values_list('title', 'description', 'image', 'slug')
        result = {number: {'title': book[0], 'description': book[1], 'image': book[2], 'slug': book[3]}
                  for number, book in enumerate(books)}
        return JsonResponse(result)


    # ВЫШЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

    if 'list_of_groups' in request.GET:
        return get_list_of_groups(request)

    if 'group' in request.GET:
        return get_group(request)
